#include "UIText.h"
#include <sfml/Graphics.hpp>
#include "glyphs.h"
#include "Math.h"
using namespace std;

static int gID = 0;

UIText::UIText( int x, int y, string text ) : mText(text)
{
	mID = gID++;
	mPosX = x;
	mPosY = y;
}


UIText::~UIText(void)
{
}

void UIText::Render()
{
	glColor3f(0.0f, 0.0f, 0.0f);
	glRasterPos2d(mPosX, mPosY);
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

	int w = 0;
	int h = GLYPH_HEIGHT;
	for (size_t i = 0; i < mText.size(); i++)
	{
		glBitmap(GLYPH_WIDTH, GLYPH_HEIGHT, 0, 0, GLYPH_WIDTH+3, 0, (GLubyte*)glyphs[mText[i]]);
		w += (GLYPH_WIDTH+3);
	}

	glFlush();

	int x = mPosX + w / 2;
	int y = mPosY + h / 2;
	
	int ox = max(40, w / 2 + 10);
	int oy = GLYPH_HEIGHT*2;

	glPushName(mID);
	glColor3f(1.0f, 1.0f, 1.0f);
	glBegin(GL_QUADS);
		glVertex3f(x - ox, y - oy, -0.5f);
		glVertex3f(x + ox, y - oy, -0.5f);
		glVertex3f(x + ox, y + oy, -0.5f);
		glVertex3f(x - ox, y + oy, -0.5f);
	glEnd();
	glPopName();
	glFlush();

	glColor3f(0.0f, 0.0f, 0.0f);
	glRasterPos2d(mPosX, mPosY);
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

	w = 0;
	h = GLYPH_HEIGHT;
	for (size_t i = 0; i < mText.size(); i++)
	{
		glBitmap(GLYPH_WIDTH, GLYPH_HEIGHT, 0, 0, GLYPH_WIDTH+3, 0, (GLubyte*)glyphs[mText[i]]);
		w += (GLYPH_WIDTH+3);
	}

	glFlush();

}
